package cn.edu.qlnu.xky.test.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import cn.edu.qlnu.xky.test.model.User;

public class UserDao {
	
	BaseDao dao = null;
	
	public UserDao() {
		dao = new BaseDao();
	}
	
	public User searchByNamePsw(String name,String psw) {
		String sql ="select * from t_user where isValid=0 and "
				+ "userName='"+name+"' and "
				+ "userPsw='"+psw+"'";
		List<User> users = this.searchBySql(sql);
		if(users.size()==0)
			return null;
		else
			return users.get(0);
	}
	
	public List<User> searchBySql(String sql){
		
		List<Map<String,Object>> objs = dao.searchBySql(sql);
		List<User> users = new ArrayList<User>();
		User u = null;
		for(Map<String,Object> obj : objs) {
			u = new User();
			
			u.setId((int)obj.get("id"));
			u.setIsValid((int)obj.get("isValid"));
			u.setTemp((String)obj.get("temp"));
			u.setType((int)obj.get("type"));
			u.setUserDisplayName((String)obj.get("userDisplayName"));
			u.setUserName((String)obj.get("userName"));
			u.setUserPsw((String)obj.get("userPsw"));
			
			users.add(u);
		}
		return users;
	}
	
	public static void main(String[] args) {
		UserDao dao = new UserDao();
		User u = dao.searchByNamePsw("zs","123");
		System.out.println(u.getUserDisplayName());
	}

}
